package com.bz.xtcx.manager.mapper;

import com.bz.xtcx.manager.entity.CmsContent;
import com.bz.xtcx.manager.mapper.provider.CmsContentProvider;
import com.bz.xtcx.manager.persist.conditions.ContentCondition;
import com.bz.xtcx.manager.vo.VoQueryCms;
import com.bz.xtcx.manager.vo.VoTaskSum;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

public interface CmsContentMapper {

    @SelectProvider(type = CmsContentProvider.class, method = "countByCategoryCodeProvider")
    int countByCategoryCode(ContentCondition cc);

    @SelectProvider(type = CmsContentProvider.class, method = "countByKeyStrOrRecommendProvider")
    int countByKeyStrOrRecommend(ContentCondition cc);

    @SelectProvider(type = CmsContentProvider.class, method = "selectByKeyStrOrRecommendProvider")
    @ResultMap("cmsContentRow")
    List<CmsContent> selectByKeyStrOrRecommend(ContentCondition cc);

    @SelectProvider(type = CmsContentProvider.class, method = "selectByCategoryCodeProvider")
    @Results(
            id = "cmsContentRow",
            value = {
                    @Result(property = "id", column = "id"),
                    @Result(property = "title", column = "title"),
                    @Result(property = "cover", column = "cover"),
                    @Result(property = "onlyUrl", column = "only_url"),
                    @Result(property = "contentUrl", column = "content_url"),
                    @Result(property = "publishDate", column = "publish_date"),
                    @Result(property = "clicks", column = "clicks"),
                    @Result(property = "categroyId", column = "categroy_id"),
                    @Result(property = "categroyCode", column = "categroy_code"),
                    @Result(property = "categroyName", column = "categroy_name"),
                    @Result(property = "typeId", column = "type_id"),
                    @Result(property = "copied", column = "copied"),
                    @Result(property = "copyFrom", column = "copy_from"),
                    @Result(property = "copyFromUrl", column = "copy_from_url"),
                    @Result(property = "author", column = "author"),
                    @Result(property = "editor", column = "editor"),
                    @Result(property = "publishNow", column = "publish_now"),
                    @Result(property = "publishUserId", column = "publish_user_id"),
                    @Result(property = "checkDate", column = "check_date"),
                    @Result(property = "checkUserId", column = "check_user_id"),
                    @Result(property = "checkStatus", column = "check_status"),
                    @Result(property = "readable", column = "readable"),
                    @Result(property = "stickSort", column = "stick_sort"),
                    @Result(property = "recommend", column = "recommend"),
                    @Result(property = "creater", column = "creater"),
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "updater", column = "updater"),
                    @Result(property = "updateTime", column = "update_time"),
                    @Result(property = "tags", column = "tags"),
                    @Result(property = "contentTxt", column = "content_txt"),
                    @Result(property = "description", column = "description"),
                    @Result(property = "cmsFileList", column = "id", many = @Many(select = "com.bz.xtcx.manager.mapper.CmsFileMapper.fingByOwnerId") ),
                    @Result(property = "fileName", column = "file_name"),
                    @Result(property = "savePath", column = "save_path"),
                    @Result(property = "reqRelUrl", column = "req_rel_url"),
                    @Result(property = "fileId", column = "file_id"),
                    @Result(property = "info", column = "info"),
                    @Result(property = "checker", column = "checker"),
                    @Result(property = "checkTime", column = "check_time")
            }
    )
    List<CmsContent> selectByCategoryCode(ContentCondition cc);

    @SelectProvider(type = CmsContentProvider.class, method = "selectByPrimaryKeyProvider")
    @ResultMap(value = "cmsContentRow")
    List<CmsContent> selectByPrimaryKey(String id);

    @Update("update cms_content set clicks = clicks + 1 where id = #{id, jdbcType=VARCHAR}")
    int updateClicks(String id);

    /**
     * @author zhangkj
     */
    @SelectProvider(type = CmsContentProvider.class, method = "findByCondition")
    List<CmsContent> findByCondition(VoQueryCms query);
    
    @Insert("insert into `cms_content`(id, title, categroy_id, type_id, cover, copied, copy_from, copy_from_url, author, editor, tags, description,"
    		+ " only_url, content_url, publish_now, publish_date, publish_user_id, check_date, check_user_id, content_txt, clicks, stick_sort,"
    		+ " recommend, check_status, readable, creater, updater)"
    		+ " values(#{id,jdbcType=VARCHAR}, #{title,jdbcType=VARCHAR}, #{categroyId,jdbcType=VARCHAR}, #{typeId,jdbcType=INTEGER}, #{cover,jdbcType=VARCHAR}, #{copied,jdbcType=INTEGER},"
    		+ " #{copyFrom,jdbcType=VARCHAR}, #{copyFromUrl,jdbcType=VARCHAR}, #{author,jdbcType=VARCHAR}, #{editor,jdbcType=VARCHAR}, #{tags,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, "
    		+ "#{onlyUrl,jdbcType=VARCHAR}, #{contentUrl,jdbcType=VARCHAR}, #{publishNow,jdbcType=INTEGER}, #{publishDate,jdbcType=DATE}, #{publishUserId,jdbcType=VARCHAR}, #{checkDate,jdbcType=DATE}, "
    		+ "#{checkUserId,jdbcType=VARCHAR}, #{contentTxt,jdbcType=VARCHAR}, #{clicks,jdbcType=INTEGER}, #{stickSort,jdbcType=INTEGER},"
    		+ " #{recommend,jdbcType=VARCHAR}, #{checkStatus,jdbcType=INTEGER}, #{readable,jdbcType=INTEGER}, #{creater,jdbcType=VARCHAR}, #{updater,jdbcType=VARCHAR})")
    @SelectKey(before = true, keyProperty = "id", resultType = String.class, statementType = StatementType.STATEMENT, statement="select uuid()")
    int insert(CmsContent e);
    
    @Delete("delete from `cms_content` where id = #{id}")
    int del(String id);
    
    @UpdateProvider(type = CmsContentProvider.class, method = "update")
    int update(CmsContent e);
    
    @Update("update `cms_content` set check_status = #{checkStatus,jdbcType=INTEGER}, check_date = #{checkDate,jdbcType=DATE}, check_user_id = #{checkUserId,jdbcType=VARCHAR} where id = #{id}")
    int updateCheckStatus(CmsContent e);
    
    @Update("update `cms_content` set readable = #{status} where id = #{id}")
    int updateStatus(@Param("status")int status, @Param("id")String id);
    
    @Update("update `cms_content` set stick_sort = #{stickSort} where id = #{id}")
    int updateStickSort(@Param("stickSort")int stickSort, @Param("id")String id);
    
    @Select("select * from `cms_content` where id = #{id}")
    @ResultMap("cmsContentRow")
    CmsContent findById(String id);
    
    @Select("select count(*) count, check_status, 2 form_type from `cms_content` where publish_user_id = #{userId} and (check_status = 0 or check_status = 2) group by check_status"
    		+ " union "
    		+ "select count(*) count, check_status, 1 form_type from `cms_exchanges` where publish_user_id = #{userId} and (check_status = 0 or check_status = 2) group by check_status")
    List<VoTaskSum> findMySubmitSum(String userId);

    @Select("select count(*) count, check_status, 2 form_type from `cms_content` where (check_status = 0 or check_status = 2) group by check_status"
            + " union "
            + "select count(*) count, check_status, 1 form_type from `cms_exchanges` where (check_status = 0 or check_status = 2) group by check_status")
    List<VoTaskSum> findAllSubmitSum();
}